from .api.Tongyi import Tongyi
import json

class TongyiAPI:
    
    def __init__(self):
        pass

    @classmethod
    def INPUT_TYPES(s):
         
        return {
            "required": {
                "image_path": ("STRING",),
              
                "API_KEY": ("STRING", {
                    "multiline": False, #True if you want the field to look like the one on the ClipTextEncode node
                    "default": "这里填写你的api-key",
                    "lazy": True
                }),
            },
        }

    RETURN_TYPES = ("STRING",)
    #RETURN_NAMES = ("image_output_name",)

    FUNCTION = "To_Tongyi"

    #OUTPUT_NODE = False

    CATEGORY = "通义API"

  
    
    def To_Tongyi(self,image_path,API_KEY):
        try:
            images = image_path
            
            print("------------image------------------",type(images))
            print("--------------string_field----------------",API_KEY)
            # image = self.To_aliyun_oss(image)

            api = API_KEY
            ty = Tongyi(api)
            text = """ """
            for index,image in enumerate(images):
                print("------------------------------",image,index)

                filejson = ty.execute_if_local_file(image)
                data = json.loads(filejson)
                content = data.get("choices", [{}])[0].get("message", {}).get("content", "")
                print(f'第{index+1}:'+content)
                text = f'作品评价:'+content
                print(type(text))
            return (str(text),)
        except:
            print("通义千问调错误")
            return 0


# A dictionary that contains all nodes you want to export with their names
# NOTE: names should be globally unique
# NODE_CLASS_MAPPINGS = {
#     "Example": TongyiAPI
# }

# # A dictionary that contains the friendly/humanly readable titles for the nodes
# NODE_DISPLAY_NAME_MAPPINGS = {
#     "Example": "通义千问API"
# }
if __name__ =="__main__":
    img_oss = 'https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg'
    TI = TongyiAPI()
    TI.To_Tongyi(img_oss)